var express = require('express');
var router = express.Router();
var Admin = require('../models/admin');
var md5=require('md5-node');

var admin_super_username = "adpushmanager"

function judge_admin_login(req){
    if (!req.session.admin){
        return false
    }
    return true
}
/* GET home page. */
router.get('/login', function(req, res, next) {
  res.render('admin/login', { title: '登录' });
});

router.post('/login', function(req, res, next) {
    res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
    if(req.body.username != admin_super_username){
        response = {"msg":"账号不存在","code":-320}
        res.end(JSON.stringify(response));
    }
    if(req.body.password.length == 0){
        response = {"msg":"密码不能为空","code":-300}
        res.end(JSON.stringify(response));
    }
  var login_admin = {
    "username":admin_super_username,
    "password":md5(req.body.password)
    };
   //查询是旧密码否正确
   Admin.findOne(login_admin, function (err, data) {
    if (data == null) {
        response = {"msg":"密码错误","code":-200}
    } else {
         req.session.admin = login_admin
         req.session.auth = true;
         response = {"msg":"success","code":1}
    }

    res.end(JSON.stringify(response));
    });
});

router.get('/index', function(req, res, next) {
   if (!judge_admin_login(req)){
       return res.redirect('/admin/login');
       // response = {"msg":"please login","code":-10000}
       // res.end(JSON.stringify(response));
   }
    // console.log(req.session)
  res.render('admin/index', { title: '默认首页' });
});
router.get('/welcome', function(req, res, next) {
   if (!judge_admin_login(req)){
       return res.redirect('/admin/login');
   }
  res.render('admin/welcome', { title: 'welcome' });
});


router.get('/editpwd', function(req, res, next) {
  res.render('admin/editpwd', { title: '修改密码' });
});

router.post('/editpwd', function(req, res, next) {
  // 输出 JSON 格式
   res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
    // if (!req.session.user) req.session.user = {"username":"uuuu","password":"123456"};

    if (!req.session.admin){
        response = {"msg":"please login","code":-10000}
        res.end(JSON.stringify(response));
    }
   // 输出 JSON 格式
   if(!req.body.old_password){
      response = {"msg":"原密码不能为空","code":-104}
      res.end(JSON.stringify(response));
   }
   if(req.body.new_password != req.body.again_password){
      response = {"msg":"新密码和重复密码不相等","code":-100}
      res.end(JSON.stringify(response));
   }

   var old_admin = {
    "username":admin_super_username,
    "password":md5(req.body.old_password)
    };
   // 查询是旧密码否正确
   Admin.findOne(old_admin, function (err, data) {
       console.log(data);
    if (data == null) {
      response = {"msg":"旧密码错误","code":-200}
    } else {
        // 保存到数据库
        // User.create(postData, function (err, data) {
        //     if (err) throw err;
        //     console.log('注册成功');
        //     res.redirect('/userList');
        // })

        var _admin = {
          "username":admin_super_username,
          "password":md5(req.body.new_password)
          };
         var admin=new Admin(_admin);

         Admin.updateOne({username:admin_super_username},{"$set":{password:md5(req.body.new_password)}},function(err,doc){
          if (err) {
            console.log(err);
            return;
          }
         });
         response = {"msg":"success","code":1}
    }
    res.end(JSON.stringify(response));
    });
   
});
module.exports = router;